//
//  LessonDetailViewController.m
//  LoveDraw
//
//  Created by 零零壹 on 2017/7/10.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "LessonDetailViewController.h"
#import "CourseHeaderView.h"
#import "CourseOneViewController.h"
#import "CourseTwoViewController.h"
#import "CourseThreeViewController.h"
@interface LessonDetailViewController ()<UIScrollViewDelegate>
{
    CourseOneViewController *soneVC;
    CourseTwoViewController *stwoVC;
    CourseThreeViewController *sthreeVC;
    UIScrollView *mainScrollView;
}
@property(strong,nonatomic)UIButton *backBtn;
@property (strong, nonatomic) CourseHeaderView *headerView;     // 上面的 view，可以自定义
@property (assign, nonatomic) CGFloat headerViewHeight;     // headerView 高度

@end

@implementation LessonDetailViewController


-(CourseOneViewController *)soneVC{
    if (soneVC==nil) {
        soneVC = [[CourseOneViewController alloc]init];
        soneVC.aid = self.sid;
        soneVC.navigationController = self.navigationController;
    }
    return soneVC;
}

-(CourseTwoViewController *)stwoVC{
    if (stwoVC==nil) {
        stwoVC = [[CourseTwoViewController alloc]init];
        stwoVC.cid = self.cid;
        stwoVC.navigationController = self.navigationController;
    }
    return stwoVC;
}

-(CourseThreeViewController *)sthreeVC{
    if (sthreeVC==nil) {
        sthreeVC = [[CourseThreeViewController alloc]init];
        sthreeVC.aid= self.sid;
        sthreeVC.navigationController = self.navigationController;
    }
    return sthreeVC;
}

-(UIButton *)backBtn
{
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_backBtn setImage:[UIImage imageNamed:@"left"] forState:(UIControlStateNormal)];
        _backBtn.frame = CGRectMake(10, 20, 40, 40);
        [_backBtn addTarget:self action:@selector(backAct) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _backBtn;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.headerViewHeight = 200;
    _headerView = [[CourseHeaderView alloc] init];
    self.headerView.frame = CGRectMake(0, 0,MAIN_SCREEN_WIDTH, self.headerViewHeight);

    [self.headerView.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,self.catethumb]] placeholderImage:Kplaceholderimage];

    [self.headerView.shadebgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,self.catethumb]] placeholderImage:Kplaceholderimage];

    [self.headerView.courseLabel setText:self.coursename];
    [self.headerView.courseTeacher setText:[NSString stringWithFormat:@"任课老师:%@",self.teachername]];

    [self.headerView.leftBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.headerView.centerBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.headerView.rightBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.backBtn];
    [self setMainSrollView];
}


-(void)setMainSrollView
{
    mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 200, KscreenW, self.view.frame.size.height)];
    mainScrollView.delegate = self;
    mainScrollView.backgroundColor = [UIColor whiteColor];
    mainScrollView.pagingEnabled = YES;
    mainScrollView.showsHorizontalScrollIndicator = NO;
    mainScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:mainScrollView];

    //将每个controller view加入到scrollerview里面
    UIView *pageview1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH)];
        [pageview1 addSubview:self.soneVC.view];
    [mainScrollView addSubview:pageview1];

    UIView *pageview2 = [[UIView alloc]initWithFrame:CGRectMake(KscreenW, 0, KscreenW, KscreenH)];
        [pageview2 addSubview:self.stwoVC.view];
    [mainScrollView addSubview:pageview2];

    UIView *pageview3 = [[UIView alloc]initWithFrame:CGRectMake(KscreenW*2, 0, KscreenW, KscreenH)];
        [pageview3 addSubview:self.sthreeVC.view];
    [mainScrollView addSubview:pageview3];

    mainScrollView.contentSize = CGSizeMake(MAIN_SCREEN_WIDTH*3, 0);
    //滚动到_currentIndex对应的tab
    [mainScrollView setContentOffset:CGPointMake(MAIN_SCREEN_WIDTH*_currentIndex, 0) animated:YES];
}

-(void)sliderAction:(UIButton *)sender{
    if (self.currentIndex==sender.tag) {
        return;
    }
    [self sliderAnimationWithTag:sender.tag];
    [UIView animateWithDuration:0.3 animations:^{
        mainScrollView.contentOffset = CGPointMake(MAIN_SCREEN_WIDTH*(sender.tag-1), 0);
    } completion:^(BOOL finished) {

    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat contentOffSetX = scrollView.contentOffset.x;
    int index_ = contentOffSetX/MAIN_SCREEN_WIDTH;
    [self sliderAnimationWithTag:index_+1];
}

-(void)sliderAnimationWithTag:(NSInteger)tag{
    if(tag==1){
        [self.headerView.leftBtn setBackgroundColor:UIColors(MainColor)];
        [self.headerView.centerBtn setBackgroundColor:[UIColor blackColor]];
        [self.headerView.rightBtn setBackgroundColor:[UIColor blackColor]];
    }else if (tag==2){
        [self.headerView.leftBtn setBackgroundColor:[UIColor blackColor]];
        [self.headerView.centerBtn setBackgroundColor:UIColors(MainColor)];
        [self.headerView.rightBtn setBackgroundColor:[UIColor blackColor]];
    }else if (tag==3){
        [self.headerView.leftBtn setBackgroundColor:[UIColor blackColor]];
        [self.headerView.centerBtn setBackgroundColor:[UIColor blackColor]];
        [self.headerView.rightBtn setBackgroundColor:UIColors(MainColor)];
    }
}

-(void)leftOnClick{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)backAct
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
